<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/1/24 15:06
 * Remark:
 */

namespace App\Service\Position;

use App\ConfigFile\Auth\AuthElement;
use App\Consts\BaseConsts\BaseConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Func\TokenFuc;
use App\Models\Department\DepartmentModel;
use App\Models\Position\PositionModel;
use Illuminate\Http\Exceptions\HttpResponseException;

class PositionService
{
    private PositionModel $_positionModel;
    private DepartmentModel $_departmentModel;

    public function __construct()
    {
        $this->_positionModel = new PositionModel();
        $this->_departmentModel = new DepartmentModel();
    }

    /**
     * 新建职位
     * @param $positionData
     * @return bool
     */
    public function createPosition($positionData): bool
    {
        $companyId = TokenFuc::getCurrentStaffInfo()['company_id'] ?? 0;
        $positionData = [
            $this->_positionModel->company_id = $companyId,
            $this->_positionModel->position_name = $positionData['position_name'],
            $this->_positionModel->position_remark = $positionData['position_remark'] ?? "",
            $this->_positionModel->department_id = $positionData['department_id'],
        ];
        $res = $this->_positionModel->createPosition($positionData);
        if(!$res){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "新建职位失败",
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS,
            ));
        }
        return true;
    }

    /**
     * 编辑职位
     * @param $positionData
     * @return mixed
     */
    public function editPosition($positionData): mixed
    {
//        unset($positionData['status']);
        unset($positionData['company_id']);
        unset($positionData['position_status']);
        unset($positionData['position_menu']);
        return $this->_positionModel->editPosition($positionData);
    }

    /**
     * 根据部门ID获取职位下拉
     * @param $departmentId
     * @return mixed
     */
    public function getPositionSelectByDepartmentId($departmentId): mixed
    {
        return $this->_positionModel->getPositionSelectByDepartmentId($departmentId,[
            'position_id',
            'position_id as key',
            'position_name',
        ]);
    }

    /**
     * 根据部门ID获取职位列表
     * @param $departmentId
     * @param $pageSize
     * @return mixed
     */
    public function getPositionListByDepartmentId($departmentId,$pageSize,$filters): mixed
    {
        $query = $this->_positionModel->select([
            'position_id',
            'position_id as key',
            'position_name',
            'department_id',
            'position_remark',
        ])->where('department_id',$departmentId)
            ->where('status',BaseConsts::STATUS_NORMAL);
        if($filters){
            $filters = json_decode($filters,true);
            if(isset($filters['position_name']) && $filters['position_name']){
                $query = $query->where('position_name','like','%'.$filters['position_name'].'%');
            }
        }
        return $this->_positionModel->getPositionListByDepartmentId($query,$pageSize);
    }

    /**
     * 根据公司Id获取职位列表
     * @param $companyId
     * @param $filters
     * @param $pageSize
     * @return mixed
     */
    public function getPositionListByCompanyId($companyId,$filters,$pageSize): mixed
    {
        // 获取公司部门信息
        $query = $this->_departmentModel->select([
            'department_id',
            'department_name',
            'department_level as department_level_name',
        ])->where('company_id',$companyId)
          ->where('status',BaseConsts::STATUS_NORMAL);
        if($filters){
            $filters = json_decode($filters,true);
            if(isset($filters['department_parent_id']) && $filters['department_parent_id']){
                $query = $query->where('department_parent_id',$filters['department_parent_id']);
            }
        }
        $departmentList = $this->_departmentModel->getDepartmentListByCompanyId($query,$pageSize)->toArray();
        // 通过部门获取职位
        if(isset($departmentList['data']) && $departmentList['data']){
            foreach($departmentList['data'] as $key => $department){
                $departmentList['data'][$key]['positions'] = $this->_positionModel->getPositionMenuByDepartmentId($department['department_id'],[
                    'position_id',
                    'position_id as key',
                    'position_name',
                    'position_remark',
                    'created_at',
                ]);
            }
        }
        return $departmentList;
    }

    /**
     *  批量删除职位
     * @param $positionIds
     * @return mixed
     */
    public function bulkDeletePosition($positionIds): mixed
    {
        return $this->_positionModel->bulkDeletePosition(
            $positionIds,[
            'status' => BaseConsts::STATUS_DELETE
        ]);
    }

    /**
     * 获取职位详情
     * @param $positionId
     * @return mixed
     */
    public function getPositionDetailByPositionId($positionId): mixed
    {
        return $this->_positionModel->getPositionInfoByPositionId(
            $positionId,[
            'position_id',
            'position_name',
            'department_id',
            'position_remark',
        ]);
    }

    /**
     * 启用职位
     * @param $positionData
     */
    public function startPosition($positionData){
        $menuIds = $positionData['menu_ids'];
        unset($positionData['elementAuthUrl']);
        unset($positionData['menu_ids']);
//        $res =  $this->_positionModel->editPosition($positionData);
//        if(!$res){
//            throw new HttpResponseException( ResponseFunc::returnJson(
//                'error',
//                null,
//                "启用职位失败",
//                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS,
//            ));
//        }
        // 获取菜单权限
        dd($menuIds);

        // 返回职位权限列表

        die();
    }



}